package com.py.pythink.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.py.pythink.bo.UserInfo;
import com.py.pythink.bo.UserSendBo;
import com.py.pythink.config.RedisKeyConfig;
import com.py.pythink.config.SystemConfig;
import com.py.pythink.dao.UserDetailDao;
import com.py.pythink.dao.UserSendDao;
import com.py.pythink.entity.User;
import com.py.pythink.entity.UserDetail;
import com.py.pythink.entity.UserSend;
import com.py.pythink.service.intf.UserSendService;
import com.py.pythink.third.JedisUtil;
import com.py.pythink.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/send/")
public class UserSendController {
    @Autowired
    private UserSendService sendService;
    @Autowired
    private UserSendDao dao;
    @Autowired
    private UserDetailDao userDetailDao;

    @PostMapping("add.do")
    public R save(UserSendBo bo, HttpServletRequest request) {
        String header = request.getHeader(SystemConfig.USER_TOKEN);
        System.err.println(JedisUtil.checkKey(RedisKeyConfig.USER_TOKEN + header));
        User user = JSON.parseObject(JedisUtil.getStr(RedisKeyConfig.USER_TOKEN + header), User.class);
        System.out.println(user);
        return sendService.insert(bo, user.getId());
    }

    @GetMapping("select.do")
    public R selectByUid(HttpServletRequest request) {
        String header = request.getHeader(SystemConfig.USER_TOKEN);
        System.err.println(header);
        System.err.println(JedisUtil.checkKey(RedisKeyConfig.USER_TOKEN + header));
        User user = JSON.parseObject(JedisUtil.getStr(RedisKeyConfig.USER_TOKEN + header), User.class);
        System.out.println(user);
        return sendService.selectByUid(user.getId());
    }

    @PostMapping("delete.do")
    public R delete(int uid, int id) {
        return sendService.delete(uid, id);
    }

    @GetMapping("person.do")
    public R sendinfo(int id) {
//        int number = dao.number(id);
//        UserDetail userDetail = userDetailDao.selectByUid(uid);
//        UserInfo userInfo = new UserInfo();
//        userInfo.setNumber(number);
        return sendService.usersendDetail(id);
    }



    @GetMapping("all.do")
    public List<UserSend> all() {
        return sendService.list();
    }

    @GetMapping("selectAll.do")
    public List<UserSend> person(){
        List<UserSend> person = dao.person();
        System.err.println(person);
        return person;
    }
    @GetMapping("gethouse.do")
    public R getHouse(){
        return sendService.getHouse();
    }

}